package cn.lizemin.flowable.service;

import cn.lizemin.flowable.entity.Role;
import cn.lizemin.flowable.entity.User;
import cn.lizemin.flowable.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author Admin
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-05-28 22:00:32
*/
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        queryWrapper.select(User::getUsername,
                User::getId,
                User::getPassword);
        User user = this.getOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户不存在");
        }
        List<Role> roles = this.getBaseMapper().getRolesByUserId(user.getId());
        user.setRoles(roles);
        return user;
    }



}




